package com.daiqee.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.daiqee.controller.support.CommonsController;
import com.daiqee.enums.RequestType;
import com.daiqee.pojo.Notice;
import com.daiqee.pojo.User;
import com.daiqee.service.impl.NoticeService;

/**
 * 公告
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V2.1
 * @author RichardTang 
 * @date: 2018年6月2日
 * @package com.daiqee.handler
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */

@Controller
@RequestMapping("notice")
public class NoticeController extends CommonsController<Notice>{

	@Autowired
	private NoticeService service;
	
	/**
	 * 扩展POST请求
	 */
	@Override
	public boolean warpperAddMethod(Notice notice) throws Exception {
		User currentUser = getCurrentUser();
		notice.setNoticePeople(currentUser.getUseAuthor());
		notice.setNoticeDate(new Date());
		return service.insert(notice);
	}

	/**
	 * 查询最新的一条公告
	 */
	@RequestMapping(value = "/new",method = RequestMethod.GET)
	public Object queryByMaxDate() {
		EntityWrapper<Notice> ew = new EntityWrapper<Notice>();
		ew.orderBy("notice_date",false);
		Notice notice = service.selectOne(ew);
		return restfulResponse.response(notice,RequestType.GET);
	 }
	
	@Override
	public boolean warpperUpdateMethod(Notice notice) {
		notice.setNoticeDate(new Date());
		return service.updateById(notice);
	}
}
